//const { querySql, queryOne } = require('../db')
const { debug } = require('../utils/constant')
const db = require('../db')
function login(username, password) {
  return db.querySql(`select * from admin_user where username='${username}' and password = '${password}'`)
}

function findUser(username) {
  return db.queryOne(`select id, username, nickname, role, avatar from admin_user where username='${username}'`)
}

async function adminInfo(){
  const admin_user=await db.querySql(`select count(id) as count from admin_user`)
  //const admin_userj=JSON.stringify(admin_user)
  ////console.log("111"+admin_userj)
  const enroll=await db.querySql(`select count(id) as count from entry_form`)
  const enroll_succes=await db.querySql(`select count(id) as count from entry_form where enroll_status="终审通过"`)
  const enroll_error=await db.querySql(`select count(id) as count from entry_form where enroll_status="审核不通过"`)
  return { admin_user:admin_user, enroll:enroll, enroll_succes:enroll_succes, enroll_error:enroll_error}
}
async function enrollInfo(post_code){
  const enroll_num=await db.querySql(`select count(id) as count from entry_form where post_code='${post_code}'`)
  //const admin_userj=JSON.stringify(admin_user)
  ////console.log("111"+admin_userj)
  //const enroll=await db.querySql(`select count(*) as count from entry_form`)
  const enroll_succes=await db.querySql(`select count(id) as count from entry_form where enroll_status="初审通过" and post_code='${post_code}'`)
  const enroll_error=await db.querySql(`select count(id) as count from entry_form where enroll_status="审核不通过" and post_code='${post_code}'`)
  return { enroll_num:enroll_num, enroll_succes:enroll_succes, enroll_error:enroll_error}
}

//插入报名信息
async function insertUser(admin_user) {
  return new Promise(async (resolve, reject) => {
      try {
          if (admin_user) {
              await db.insert(admin_user, 'admin_user')//添加的表名
              resolve()

          } else {
              reject(new Error('添加的报名信息不正确'))
          }
      } catch (e) {
          reject(e)
      }
  })
}
//获取单个报名信息
function getOneUser(id) {
  return db.queryOne(`select * from admin_user where id ='${id}' `)
}
function getOneUserPassword(id,idd) {
  return db.queryOne(`select count(id) as count from admin_user where username ='${id}' and email ='${idd}' `)
}
//获取单个报名信息，用用户名查询
async function getOneUserName(id) {
  return await db.queryOne(`select count(id) as count from admin_user where username ='${id}' `)
}


//更新报名信息
function updateUser(Enroll) {
  return new Promise(async (resolve, reject) => {
      try {
          if (Enroll) {
              await db.update(Enroll, 'admin_user' ,`where id='${Enroll.id}'`)//添加的表名
              resolve()
          } else {
              reject(new Error('修改的用户信息不正确'))
          }
      } catch (e) {
          reject(e)
      }
  })
}
//更新用户密码
function updateUserPassword(Enroll) {
  return new Promise(async (resolve, reject) => {
      try {
          if (Enroll) {
              await db.update(Enroll, 'admin_user' ,`where id='${Enroll.id}'`)//添加的表名
              resolve()
          } else {
              reject(new Error('修改的用户信息不正确'))
          }
      } catch (e) {
          reject(e)
      }
  })
}
//更新忘记用户密码
function updateUserPassword(username,password) {
  return new Promise(async (resolve, reject) => {
      try {
          if (password) {
              await db.update(password, 'admin_user' ,`where username='${username}'`)//添加的表名
              resolve()
          } else {
              reject(new Error('修改的用户信息不正确'))
          }
      } catch (e) {
          reject(e)
      }
  })
}
//删除报名信息
function deleteUser(newsId) {
  return new Promise(async (resolve, reject) => {
      if(newsId){
          const sql = `delete from admin_user where id='${newsId}'`
          db.querySql(sql).then(() => {
              resolve()
          })}
      else {
          reject(new Error('删除用户信息失败'))
      }
  })
}
//获取报名信息列表
async function UserList(query) {
  debug && console.log(query)
  const {
    nickname,
    username,
    sort,
    page = 1,
    pageSize = 20
  } = query
  const offset = (page - 1) * pageSize
  let newsSql = 'select * from admin_user'
  let where = 'where'
  nickname && (where = db.andLike(where, 'nickname', nickname))
  username && (where = db.and(where, 'username', username))
  if (where !== 'where') {
    newsSql = `${newsSql} ${where}`
  }
  if (sort) {
    const symbol = sort[0]
    const column = sort.slice(1, sort.length)
    const order = symbol === '+' ? 'asc' : 'desc'
    newsSql = `${newsSql} order by \`${column}\` ${order}`
  }
  let countSql = `select count(*) as count from admin_user`
  if (where !== 'where') {
    countSql = `${countSql} ${where}`
  }
  const count = await db.querySql(countSql)
  newsSql = `${newsSql} limit ${pageSize} offset ${offset}`
  const list = await db.querySql(newsSql)
  //list.forEach(news => news.cover = Book.genCoverUrl(book))
  return { list, count: count[0].count, page, pageSize }
}

//获取招聘计划信息列表
async function PlanList() {
  //debug && console.log(query)
  
  let newsSql = 'select * from plan'
  
  
  const list = await db.querySql(newsSql)
  //list.forEach(news => news.cover = Book.genCoverUrl(book))
  return list
}

module.exports = {
  login,
  findUser,
  insertUser,
  getOneUser,
  updateUser,
  deleteUser,
  UserList,
  getOneUserName,
  getOneUserPassword,
  updateUserPassword,
  adminInfo,
  enrollInfo,
  PlanList
}
